# encoding: utf-8
"""
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    created by lane.chang on '15/11/2023'
    comment: 阿里云语音通知服务相关
"""
from alibabacloud_tea_openapi.client import Client as OpenApiClient
from alibabacloud_tea_openapi import models as open_api_models
from alibabacloud_tea_util import models as util_models
from alibabacloud_openapi_util.client import Client as OpenApiUtilClient

from project.lib.common import logger
from project.core.log import Colors


class AliTel:

    def __init__(self):
        pass

    @staticmethod
    def create_client() -> OpenApiClient:
        """
        使用AK&SK初始化账号Client
        @return: Client
        @throws Exception
        """
        # 工程代码泄露可能会导致 AccessKey 泄露，并威胁账号下所有资源的安全性。以下代码示例仅供参考。
        # 建议使用更安全的 STS 方式，更多鉴权访问方式请参见：https://help.aliyun.com/document_detail/378659.html。
        config = open_api_models.Config(
            # 必填，请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_ID。,
            access_key_id='LTAI5tPvsCxYSqnw6KrA84De',
            # 必填，请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_SECRET。,
            access_key_secret='xp2mTprb6ZAPfGSSQTBiFVQBxrgpTV'
        )
        # Endpoint 请参考 https://api.aliyun.com/product/Dyvmsapi
        config.endpoint = f'dyvmsapi.aliyuncs.com'
        config.region_id = 'cn-shenzhen'
        return OpenApiClient(config)

    @staticmethod
    def create_api_info() -> open_api_models.Params:
        """
        API 相关
        @param path: string Path parameters
        @return: OpenApi.Params
        """
        params = open_api_models.Params(
            # 接口名称,
            action='SingleCallByTts',
            # 接口版本,
            version='2017-05-25',
            # 接口协议,
            protocol='HTTPS',
            # 接口 HTTP 方法,
            method='POST',
            auth_type='AK',
            style='RPC',
            # 接口 PATH,
            pathname=f'/',
            # 接口请求体内容格式,
            req_body_type='json',
            # 接口响应体内容格式,
            body_type='json'
        )
        return params

    @staticmethod
    def send(called_number, tts_code, tts_param=None, volume=100, speed=0, play_times=2):
        """ 发送语音通知
        :param called_number: 被呼叫者
        :param tts_code:
        :param tts_param:
        :param volume: 音量
        :param speed: 250
        :param play_times: 播放次数
        :return:
        """
        client = AliTel.create_client()
        params = AliTel.create_api_info()

        query = {
            'CalledNumber': called_number,
            'TtsCode': tts_code,
            'Volume': volume,
            'Speed': speed,
            'PlayTimes': play_times
        }
        if tts_param is not None:
            query['TtsParam'] = tts_param

        runtime = util_models.RuntimeOptions()
        request = open_api_models.OpenApiRequest(
            query=OpenApiUtilClient.query(query)
        )

        logger.info(f'发送电话通知: called_number: {called_number} tts_code: {tts_code} tts_param: {tts_param}', font_color=Colors.PURPLE.value)
        client.call_api(params, request, runtime)


if __name__ == "__main__":

    AliTel.send('19926544477', 'TTS_313430028', tts_param='{"sevice_address": "乐成系统", "sevice_name": "用品补充"}')
    #
    # client = AliTel.create_client()
    # params = AliTel.create_api_info()
    # # query params
    # queries = {}
    # queries['CalledNumber'] = '13288022717'
    # queries['TtsCode'] = 'TTS_313430028'
    # queries['TtsParam'] = '{"sevice_address": "乐成服务", "sevice_name": "用品补充"}'
    # # runtime options
    # runtime = util_models.RuntimeOptions()
    # request = open_api_models.OpenApiRequest(
    #     query=OpenApiUtilClient.query(queries)
    # )
    # # 复制代码运行请自行打印 API 的返回值
    # # 返回值实际为 Map 类型，可从 Map 中获得三类数据：响应体 body、响应头 headers、HTTP 返回的状态码 statusCode。
    # client.call_api(params, request, runtime)

